package com.zcbl.toolkit.logger;

import com.zcbl.toolkit.http.request.RequestMetaData;
import org.springframework.scheduling.annotation.Async;
import org.springframework.util.Assert;

import java.util.Date;


/**
 * @author he peng
 * @create 2018/5/7 13:14
 * @see
 */
public class SurveyBizDBLogger {

    private static final ThreadLocal<RequestMetaData> THREAD_LOCAL = new ThreadLocal<>();
    private SurveyCaseOperateDAO surveyCaseOperateDAO;

    public SurveyBizDBLogger(SurveyCaseOperateDAO surveyCaseOperateDAO) {
        this.surveyCaseOperateDAO = surveyCaseOperateDAO;
    }

    public static void setRequestMetaData(RequestMetaData requestMetaData) {
        THREAD_LOCAL.set(requestMetaData);
    }

    public static RequestMetaData getRequestMetaData() {
        return THREAD_LOCAL.get();
    }

    @Async("singleThreadExecutor")
    public synchronized SurveyBizDBLogger log(SurveyCaseOperate logData) {
        Assert.notNull(logData , "logData == null");
        logData.setCreateTime(new Date());
        surveyCaseOperateDAO.insertSelective(logData);
        return this;
    }
}
